﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Net.Mail;
using System.Net;


public partial class Admin : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (Session["Logged"].Equals("No"))
        {
            Response.Redirect("Default.aspx");
        }

        if (Session["Email"] == "")
            LinqDataSource1.Where = "";
        else
            LinqDataSource1.Where = "Email == @Email";
    }
    protected void Bt_Logout_Click(object sender, EventArgs e)
    {
        Session["Logged"] = "No";
        Session["User"] = "";
        Response.Redirect("Default.aspx");
    }
    protected void Bt_EmailSearch_Click(object sender, EventArgs e)
    {
        string HTML_email = String.Format("{0}", Request.Form[@"Txt_Email"]);

        Session["Email"] = HTML_email;
    }

    protected void Bt_DownloadLink_Click(object sender, EventArgs e)
    {
        string script;
        string downloadEmail = String.Format("{0}", Request.Form[@"Txt_Downloademail"]).ToLower();
        if (downloadEmail == "")
            return;

        string emailEnd = downloadEmail.Substring(downloadEmail.IndexOf('@') + 1);

        if (emailEnd == "")
            return;
        if (emailEnd == "worldbank.org")
        {
            SmtpClient smtpSender = new SmtpClient("smtp.gmail.com", 587);
            smtpSender.Credentials = new NetworkCredential("worldbank@thetigerparty.com", "gogotiger");
            smtpSender.EnableSsl = true;

            MailMessage msg = new MailMessage();
            msg.To.Add(downloadEmail);
            msg.From = new MailAddress("M-APP Photo Diary <wbmapp@gmail.com>");

            msg.Subject = "M-APP Photo Diary";
            string htmlBody = @"<br><br>You can download the latest version of the M-APP Photo Diary from the link below:<br><a href=\""http://devtest.wearepassionfruit.com/1.0.3/WorldBank.jad\"">http://devtest.wearepassionfruit.com/1.0.3/WorldBank.jad</a><br><br>Thank you and please let us know what you think.<br><br><img src=\""http://worldbank.thetigerparty.com/images/Mapp_Logo.png\"" style=\""display:block\"" />";
            msg.Body = htmlBody;
            msg.IsBodyHtml = true;

            try
            {
                smtpSender.Send(msg);
            }
            catch (Exception ex)
            {
                Console.WriteLine("GAN!:{0}", ex.ToString());
            }

            script = @"alert(""Download Link for the App has been sent to " + downloadEmail + "\")";
            ScriptManager.RegisterClientScriptBlock(DownloadPanel, typeof(UpdatePanel), "jscript", script, true);
        }
        else
        {
            script = @"alert(""Currently we are only accepting download request from @WorldBank.org"")";
            ScriptManager.RegisterClientScriptBlock(DownloadPanel, typeof(UpdatePanel), "jscript", script, true);
        };
    }

    protected void LinqDataSource1_Inserting(object sender, LinqDataSourceInsertEventArgs e)
    {

        FileUpload imageUpload = (FileUpload)ListView1.InsertItem.FindControl("ImageUpload");
        string path = Server.MapPath(@"images\UploadImage\" + imageUpload.FileName);

        if (imageUpload.HasFile)
        {
            try
            {
                imageUpload.SaveAs(path);
            }
            catch (Exception err)
            {
                e.Cancel = true;
            }
        }

        BB_UPLOAD_DATA submitImage = (BB_UPLOAD_DATA)e.NewObject;
        submitImage.ImageName = @"images/UploadImage/" + imageUpload.FileName;
    }
}